﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PublishData.DAL;

namespace PublishData.BLL
{
    public class HoSoBLL
    {
        TCHSHueDataContext data = FormThietLapChuoiKetNoi.AppDbConnection;

        public HoSoBLL()
        {
            //data.ObjectTrackingEnabled = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sbn"></param>
        /// <param name="dt"></param>
        /// <param name="mcv"></param>
        /// <param name="ht"></param>
        /// <param name="dc"></param>
        /// <param name="sto"></param>
        /// <param name="sthua"></param>
        /// <param name="phuong"></param>
        /// <param name="mabxl"></param>
        /// <returns>0:Lỗi;1:Thêm mới;2:cập nhật</returns>
        internal int update(string sbn, string dt, string mcv, string ht, string dc, string sto, string sthua, string phuong, string mabxl)
        {
            try
            {
                int intMcv = int.Parse(mcv);
                var hs = (from e in data.HoSos
                          where e.maBN == sbn
                          select e).SingleOrDefault();
                if (hs == null)
                {
                    //Tiến hành thêm mới
                    hs = new HoSo();
                    hs.maBN = sbn.Truncated(29);
                    hs.soDienThoai = dt.Truncated(19);
                    hs.maTrangThai = intMcv;
                    hs.hoTen = ht.Truncated(49);
                    hs.diaChi = dc.Truncated(99);
                    hs.soTo = sto.Truncated(9);
                    hs.soThua = sthua.Truncated(9);
                    hs.phuong = phuong.Truncated(49);
                    hs.maBXL = int.Parse(mabxl);
                    data.HoSos.InsertOnSubmit(hs);
                    //data.SubmitChanges();
                    return 1;
                }
                else if (hs.maTrangThai != intMcv)
                {
                    hs.soDienThoai = dt.Truncated(19);
                    hs.maTrangThai = intMcv;
                    hs.hoTen = ht.Truncated(49);
                    hs.diaChi = dc.Truncated(99);
                    hs.soTo = sto.Truncated(9);
                    hs.soThua = sthua.Truncated(9);
                    hs.phuong = phuong.Truncated(49);
                    hs.maBXL = int.Parse(mabxl);
                    //data.SubmitChanges();
                    return 2;
                }
                else
                    return 3;
            }
            catch
            {
                return 0;
            }
        }
        
        public void Luu()
        {
            data.SubmitChanges();
        }
    }
}
